<?php
/**
 * Created by PhpStorm.
 * User: kkw
 * Date: 2019/1/27
 * Time: 13:05
 */

namespace kkw\user\response;
use kkw\user\table\UserTable;
use kkw\user\Util;
use suda\core\Request;
use suda\core\Response;

class LoginResponse extends  Response
{
    public function onRequest(Request $request)
    {   Util::printHeader();
        $json =  Request::json();
        $name = $json['name'];
        $user = new UserTable();
        $data = $user->select(['id', 'password','type'], ['name' => $name]) ->fetch();
        if  ($data != null) {
            $password = $data['password'];
            if ($password === md5($json['pwd'])) {
                Util::userSign($data['id']);
                $this->json([
                    'code' => 0,
                    'message'  => '登录成功'
                ]);
            }else{
                $this->json([
                    'code' => 2,
                    'message'  => '登录失败，请重试'
                ]);
            }
        }
        else{
            // 查询到的数据为空时
            $this->json([
                'code' => 1,
                'message'  => '当前用户不存在'
            ]);
        }



        if($json['name'] === 'admin'&& $json['pwd']=== 'admin'){
            $this->json([
                'data' => $json,
                'code' => 0,
                'message'  => '登录成功'
            ]);
        } else {

        }
    }
}